var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var Token = require('./utils/jwtutil')

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var goodsRouter = require('./routes/goods');
var operatorRouter = require('./routes/operator');
var testRouter = require('./routes/test');
var sysRouter = require('./routes/sys');
var messageRouter = require('./routes/message');

var app = express();
var cors = express('cors')
app.use(cors)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.all('*', function (req, res) {
  res.header("Access-Control-Allow-Headers", "*");
  res.setHeader('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'GEt,POST,PUT')
  // res.header('Access-Control-Allow-Headers', 'Content-Type')
  res.header('Content-Type', 'multipart/form-data')
  res.header('Content-Type', 'application/json;charset=utf-8')
  // authorization
  console.log(req.url);
  let arr = ['/user/login', '/user/register','/operator/login'] //数组元素为不需要token认证url
  if (arr.indexOf(req.url) !== -1) {
    req.next()
    return
  }
  // // token认证
  // let token = req.headers.authorization
  // console.log('token>>>', token);
  // if (!token) {
  //   res.send({
  //     code: -1,
  //     info: 'token不存在!'
  //   })
  //   return
  // }
  // let tokenObj = Token.decrypt(token)
  // console.log('tokenObj.token>>>', !tokenObj.token);
  // if (!tokenObj.token) {
  //   res.send({
  //     code: -1,
  //     info: 'token验证失败!'
  //   })
  //   return
  // }
  req.next()

})

//请求的拦截,all所有请求方法,/*表示任何路径都到这里
// app.all('/*', function (req, res, next) {

//   // // 过滤不需要token认证
//   // let arr = ['/user/login', '/user/register'] //数组元素为不需要token认证url
//   // if (arr.indexOf(req.url) !== -1) {
//   //   next()
//   //   return
//   // }
//   // token认证
//   // let token = req.headers.authorization
//   // console.log('token>>>',token);
//   // if (!token) {
//   //   res.send({
//   //     code: -1,
//   //     info: 'token不存在!'
//   //   })
//   //   return
//   // }
//   // let tokenObj = Token.decrypt(token)
//   // console.log('tokenObj.token>>>',tokenObj.token);
//   // if (!tokenObj.token) {
//   //   res.send({
//   //     code: -1,
//   //     info: 'token验证失败!'
//   //   })
//   //   return
//   // }
//   // next()
// })

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/goods', goodsRouter);
app.use('/operator', operatorRouter);
app.use('/test', testRouter);
app.use('/sys', sysRouter);
app.use('/message', messageRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;